﻿using ArtMath.Component.Point;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Timers;

namespace ArtMath.Animation
{
    [Serializable]
    public class NormalAnimation : AnimationBase
    {
        [NonSerialized]
        Timer timer;
        public NormalAnimation(PointBase pt, string linespeed, int frmRate = 25, Direction dir = Direction.Forward) : base(pt, linespeed, frmRate, dir)
        {
            Timer = new Timer();
        }
        void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            Move();
            Duration += timePerElapse;
        }
        [Browsable(false)]
        public System.Timers.Timer Timer
        {
            get { return timer; }
            private set
            {
                if (timer != null)
                {
                    timer.Elapsed -= Timer_Elapsed;
                }
                timer = value;
                if (timer != null)
                {
                    timer.Elapsed += Timer_Elapsed;
                    FrameRate = FrameRate;
                }
            }
        }
        public override int FrameRate
        {
            get
            {
                return base.FrameRate;
            }

            set
            {
                base.FrameRate = value;
                timer.Interval = 1e3 / FrameRate;
                timePerElapse = timer.Interval / 1e3;
            }
        }
        public override bool IsRunning
        {
            get
            {
                return timer.Enabled;
            }
        }
        public override void Run()
        {
            base.Run();
            timer.Start();
        }
        public override void Stop()
        {
            timer.Stop();
            base.Stop();
        }
        public override void AfterDeserialized(Dictionary<string, object> tag)
        {
            base.AfterDeserialized(tag);
            Timer = new Timer();
        }
    }
}
